package com.ruoyi.mobile.api;


import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qsng.domain.KClearinghouse;
import com.ruoyi.qsng.domain.KCourse;
import com.ruoyi.qsng.domain.KCoursePurchase;
import com.ruoyi.qsng.domain.TRefund;
import com.ruoyi.qsng.service.IKCoursePurchaseService;
import com.ruoyi.qsng.service.IKCourseService;
import com.ruoyi.qsng.service.ITRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *  退款
 *
 * @author gk
 * @date 2023-07-18
 */
@Api(tags = "退款")
@RestController
@RequestMapping
public class RefundController {

    @Autowired
    private ITRefundService tRefundService;

    @Autowired
    private IKCoursePurchaseService kCoursePurchaseService;

    @Autowired
    private IKCourseService kCourseService;

    @ApiOperation(value = "新增退款")
    @PostMapping("/weixin/addRefund/{orderId}")
    public AjaxResults addRefund(@PathVariable("orderId") String orderId) throws ParseException
    {
        Long id = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(id, "获取用户id失败！");

        Assert.notNull(orderId, "获取订单id失败！");

        KCoursePurchase kCoursePurchase =kCoursePurchaseService.selectKCoursePurchaseById(orderId);

        if(kCoursePurchase.getState() ==0){
            return  AjaxResults.error("该订单未支付，无法退款！");
        }

        if(!kCoursePurchase.getUserId().equals(String.valueOf(id))){
            return  AjaxResults.error("该订单不是您购买，无法退款！");
        }
        //判断是否已经开课 开课无法退费
        KCourse kCourse =kCourseService.selectKCourseById(kCoursePurchase.getCourseId());
         int sign=kCourse.getKkKksj().compareTo(new Date());
         if(sign < 0){
             return  AjaxResults.error("课程已经开始无法退款！");
         }

        TRefund tRefund1=tRefundService.selectTRefundByOrderId(kCoursePurchase.getId());

        if(null!=tRefund1 ) {
            if (tRefund1.getState() == 0) {
                return AjaxResults.error("该订单正在退款中！");
            }else if (tRefund1.getState() == 1) {
                return AjaxResults.error("该订单已退款！");
            }else {
                tRefund1.setState((long) 0);
                return AjaxResults.success(tRefundService.updateTRefund(tRefund1) );
            }

        }else {
            TRefund tRefund = new TRefund();
            tRefund.setOrderId(kCoursePurchase.getId());
            tRefund.setUserId(String.valueOf(id));
            tRefund.setMoney(kCoursePurchase.getPrice());
            tRefund.setState((long) 0);
            return AjaxResults.success(tRefundService.insertTRefund(tRefund) );
        }





    }

}
